<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true">
        <el-form-item label="支付总计：">
          {{ total.currency }} {{ total.total_amount }}
        </el-form-item>
        <el-form-item label="手续费总计：">
          {{ total.currency }} {{ total.total_fee }}
        </el-form-item>
        <el-form-item label="入账总计：">
          {{ total.currency }} {{ total.total_settlement_amount }}
        </el-form-item>
        <el-form-item label="退款总计：">
          {{ total.currency }} {{ total.total_refund_amount }}
        </el-form-item>
      </el-form>
    </div>
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item>
          <el-input
            placeholder="支付渠道id"
            v-model="form.channel_id"
            style="width: 100px"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            placeholder="商户id"
            v-model="form.merchant_id"
            style="width: 120px"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            placeholder="订单号"
            v-model="form.order_no"
            style="width: 260px"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="form.currency"
            placeholder="币种"
            clearable
            style="width: 120px"
          >
            <el-option
              v-for="item in currencyList"
              :key="item.name"
              :label="item.alias"
              :value="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="form.status"
            placeholder="订单状态"
            clearable
            style="width: 120px"
          >
            <el-option
              v-for="item in statusList"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="form.is_deposit"
            placeholder="入账状态"
            clearable
            style="width: 120px"
          >
            <el-option
              v-for="item in isDeposit"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="下单时间">
          <el-date-picker
            v-model="form.timeData"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="截止日期"
            :default-time="pickerDefaultTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column
            prop="merchant_id"
            label="商户id"
            align="center"
            width="100"
          />
          <el-table-column
            prop="merchant_name"
            label="商户名称"
            align="center"
            width="150"
          />
          <el-table-column
            prop="pay_channel_id"
            label="通道id"
            align="center"
          />
          <el-table-column
            prop="pay_channel_name"
            label="通道名称"
            align="center"
          />
          <el-table-column
            prop="order_no"
            label="系统订单号"
            align="center"
            width="150"
          />
          <el-table-column
            prop="merchant_order_no"
            label="商户订单号"
            align="center"
            width="150"
          />
          <el-table-column
            prop="pay_channel_order_no"
            label="支付通道订单号"
            align="center"
            width="150"
          />
          <el-table-column prop="status" label="状态" align="center" />
          <el-table-column
            prop="amount"
            label="支付金额"
            align="center"
            width="100"
          >
            <template #default="scope"
              >{{ scope.row.amount }} {{ scope.row.currency }}</template
            >
          </el-table-column>
          <el-table-column
            prop="refund_real_amount"
            label="退款金额"
            align="center"
            width="100"
          >
            <template #default="scope"
              >{{ scope.row.refund_real_amount }}
              {{ scope.row.currency }}</template
            >
          </el-table-column>
          <el-table-column prop="fee" label="手续费" align="center" width="100">
            <template #default="scope"
              >{{ scope.row.fee }} {{ scope.row.currency }}</template
            >
          </el-table-column>
          <el-table-column
            prop="settlement_amount"
            label="入账金额"
            align="center"
            width="120"
          >
            <template #default="scope"
              >{{ scope.row.settlement_amount }}
              {{ scope.row.settlement_currency }}</template
            >
          </el-table-column>
          <el-table-column prop="is_deposit" label="是否入账" align="center">
            <template #default="scope">
              <el-tag :type="scope.row.is_deposit ? 'success' : 'danger'">{{
                scope.row.is_deposit ? "已入账" : "未入账"
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="ip"
            label="IP地址"
            align="center"
            width="150"
          />
          <el-table-column
            prop="createtime"
            label="下单时间"
            align="center"
            :formatter="formatterTime"
            width="180"
          />
          <el-table-column
            prop="pay_time"
            label="成功时间"
            align="center"
            :formatter="formatterTime"
            width="180"
          />
          <el-table-column
            prop="deposit_time"
            label="入账时间"
            align="center"
            :formatter="formatterTime"
            width="180"
          />
          <el-table-column
            prop="expire_time"
            label="过期时间"
            align="center"
            :formatter="formatterTime"
            width="180"
          />
          <!--<el-table-column prop="description" label="备注" align="center" :formatter="formatterTime" />-->
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import {
  formatterTime,
  exportOrDownload,
  timestamp,
  pickerDefaultTime,
} from "@/utils/utils";
import { payVAOrderListApi } from "@/api/payServerse/payorder";
import { assetsCurrencyApi } from "@/api/payServerse/settings";
const loading = ref(false);
const tableData = ref([]);
const statusList = ref([]);
const isDeposit = ref([]);
const currencyList = ref([]);
const formData = {
  channel_id: "",
  merchant_id: "",
  order_no: "",
  status: "",
  currency: "",
  is_deposit: "",
  timeData: [],
};
const form = reactive({ ...formData });

const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
const total = reactive({
  total_amount: 0,
  total_fee: 0,
  total_settlement_amount: 0,
  total_refund_amount: 0,
});

//参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    order_no: form.order_no,
    status: form.status,
    is_deposit: form.is_deposit,
    channel_id: form.channel_id,
    merchant_id: form.merchant_id,
    currency: form.currency,
    start_time: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end_time: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
  };
};
// 获取币种
const getCurrencyList = async () => {
  try {
    const res = await assetsCurrencyApi({});
    currencyList.value = res.data.results;
  } catch (error) {
    currencyList.value = [];
  }
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await payVAOrderListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
    statusList.value = res.data.extra.status.map((item) => {
      return { type: item.value, name: item.name };
    });
    isDeposit.value = res.data.extra.is_deposit.map((item) => {
      return { type: item.value, name: item.name };
    });
    total.currency = res.data.extra.total?.currency;
    total.total_amount = res.data.extra.total?.total_amount;
    total.total_fee = res.data.extra.total?.total_fee;
    total.total_settlement_amount =
      res.data.extra.total?.total_settlement_amount;
    total.total_refund_amount = res.data.extra.total?.total_refund_amount;
  } catch (error) {
    loading.value = false;
  }
};
//查询
const onQuery = async () => {
  getTbaleData();
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTbaleData();
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1, pagesize: 9999999 });
  const res = await payVAOrderListApi(data, "blob");
  exportOrDownload(res, "PayFaz-VA订单列表");
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};

// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
  getCurrencyList();
});
</script>
